package com.chixing.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SeatStatusService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 锁定座位（临时锁定，设置过期时间）
     * @param scheduleId 场次ID
     * @param seatCoordinates 座位坐标列表（逗号分隔）
     * @return 是否锁定成功
     */
    public boolean lockSeatsTemporarily(Integer scheduleId, String seatCoordinates) {
        try {
            String[] seats = seatCoordinates.split(",");
            for (String coordinate : seats) {
                String key = "seat:" + scheduleId + ":" + coordinate.trim();
                // 设置座位状态为1(已锁座)，并设置15分钟过期时间
                redisTemplate.opsForValue().set(key, 1, 15, TimeUnit.MINUTES);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 确认预订座位（永久锁定，不设置过期时间）
     * @param scheduleId 场次ID
     * @param seatCoordinates 座位坐标列表（逗号分隔）
     * @return 是否更新成功
     */
    public boolean confirmBookingPermanently(Integer scheduleId, String seatCoordinates) {
        try {
            String[] seats = seatCoordinates.split(",");
            for (String coordinate : seats) {
                String key = "seat:" + scheduleId + ":" + coordinate.trim();
                // 更新座位状态为2(已预定)，不设置过期时间
                redisTemplate.opsForValue().set(key, 2);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 释放座位（支付失败或超时）
     * @param scheduleId 场次ID
     * @param seatCoordinates 座位坐标列表（逗号分隔）
     * @return 是否释放成功
     */
    public boolean releaseSeats(Integer scheduleId, String seatCoordinates) {
        try {
            String[] seats = seatCoordinates.split(",");
            for (String coordinate : seats) {
                String key = "seat:" + scheduleId + ":" + coordinate.trim();
                // 删除座位状态键，使其变为空置状态
                redisTemplate.delete(key);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 获取指定场次和座位的当前状态
     * @param scheduleId 场次ID
     * @param seatCoordinate 座位坐标
     * @return 座位状态（0-空闲，1-临时锁定，2-永久锁定）
     */
    public Integer getSeatStatus(Integer scheduleId, String seatCoordinate) {
        try {
            String key = "seat:" + scheduleId + ":" + seatCoordinate.trim();
            Object status = redisTemplate.opsForValue().get(key);

            if (status == null) {
                return 0; // 空闲状态
            }

            return Integer.valueOf(status.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return -1; // 错误状态
        }
    }


    /**
     * 检查座位是否可预订
     * @param scheduleId 场次ID
     * @param seatCoordinate 座位坐标
     * @return 是否可预订
     */
    public boolean isSeatAvailable(Integer scheduleId, String seatCoordinate) {
        String key = "seat:" + scheduleId + ":" + seatCoordinate;
        Object status = redisTemplate.opsForValue().get(key);
        // 如果key不存在或者状态为0，则座位可用
        return status == null || "0".equals(status.toString());
    }
    /**
     * 取消座位锁定（订单取消时调用）
     * @param scheduleId 场次ID
     * @param seatCoordinates 座位坐标列表（逗号分隔）
     * @return 是否取消成功
     */
    public boolean cancelBooking(Integer scheduleId, String seatCoordinates) {
        try {
            String[] seats = seatCoordinates.split(",");
            for (String coordinate : seats) {
                String key = "seat:" + scheduleId + ":" + coordinate.trim();
                // 删除座位状态键，使其变为空置状态
                redisTemplate.delete(key);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
